package edu.hawaii.ics414;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Initializes the game view screen when a game is selected from the main screen.
 * 
 * @author Matthew Mizumoto
 * 
 */
public class GameView extends Activity {

  /*
   * Called when the activity is first created.
   */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_view);
    Intent intent = getIntent();
    String gameName = intent.getStringExtra("gameName");
    GameInfo game = null;
    String[] comments = null;
    int count = 0;
    double total = 0;
    int viewCount = 0;
    boolean countUpdated = false;
    ArrayList<String> splitComments = new ArrayList<String>();
    try {
      game = new GetGameInfo().execute(gameName).get(5, TimeUnit.SECONDS);
      comments = new GetGameComments().execute(gameName).get(5, TimeUnit.SECONDS);
      count = new GetTotalComments().execute(gameName).get(5, TimeUnit.SECONDS);
      total = new GetTotalRating().execute(gameName).get(5, TimeUnit.SECONDS);
      viewCount = new GetCurrentViews().execute(gameName).get(5, TimeUnit.SECONDS);
      countUpdated =
          new UpdateViewsCount().execute(gameName, String.valueOf(viewCount + 1)).get(5,
              TimeUnit.SECONDS);
      Log.wtf("", "count: " + count + "total: " + total);

      Toast.makeText(this, "Views: " + viewCount, Toast.LENGTH_LONG).show();
      Toast.makeText(this, "Views Update: " + countUpdated, Toast.LENGTH_LONG).show();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    String formattedComments = "";

    for (int i = 1; i < comments.length; i++) {
      String[] temp = comments[i].split("\\|\\|\\|");
      splitComments.add(temp[0]);
      splitComments.add(temp[1]);
      splitComments.add(temp[2]);
    }

    for (int i = 0; i < splitComments.size(); i += 3) {
      formattedComments += splitComments.get(i) + "\n";
      formattedComments += splitComments.get(i + 1) + "\n";
      formattedComments += splitComments.get(i + 2) + "\n\n\n";
    }

    TextView name = (TextView) findViewById(R.id.gameName);
    name.setText(gameName);

    TextView userRating = (TextView) findViewById(R.id.userRatingScore);

    if (count > 0) {
      userRating.setText(String.valueOf(total / count));
    }
    else {
      userRating.setText("0");
    }

    TextView metacriticScore = (TextView) findViewById(R.id.metacriticScore);
    metacriticScore.setText(String.valueOf(game.getMetacriticRating()));

    TextView ignScore = (TextView) findViewById(R.id.ignScore);
    ignScore.setText(String.valueOf(game.getIGNRating()));

    TextView gamespotScore = (TextView) findViewById(R.id.gamespotScore);
    gamespotScore.setText(String.valueOf(game.getGamespotRating()));

    TextView allComments = (TextView) findViewById(R.id.allComments);
    allComments.setText(formattedComments);
  }

  @Override
  /*
   * Sets the menu when the user presses the menu button (non-Javadoc)
   * 
   * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
   */
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_comment, menu);
    return true;
  }

  @Override
  /*
   * Before the menu is created, sets the menu properties. (non-Javadoc)
   * 
   * @see android.app.Activity#onPrepareOptionsMenu(android.view.Menu)
   */
  public boolean onPrepareOptionsMenu(Menu menu) {
    if (CurrentUser.isLoggedIn) {
      menu.findItem(R.id.menu_comment).setEnabled(true);
      menu.findItem(R.id.menu_favorite).setEnabled(true);
    }
    return true;
  }

  @Override
  /*
   * Opens the appropriate screen when a menu option is selected. (non-Javadoc)
   * 
   * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
   */
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_comment:
      Intent myIntentProfile = new Intent(GameView.this, CommentView.class);
      Intent intent = getIntent();
      String gameName = intent.getStringExtra("gameName");
      myIntentProfile.putExtra("gameName", gameName);
      startActivityForResult(myIntentProfile, 0);
      return true;
    case R.id.menu_favorite:
      boolean added;
      boolean counted;
      int currentFavNum = 0;
      try {
        Intent intent2 = getIntent();
        String gameName2 = intent2.getStringExtra("gameName");
        added =
            new AddToFavorites().execute(CurrentUser.username, gameName2).get(5, TimeUnit.SECONDS);

        if (added) {
          Toast.makeText(this, "Added to Favorites", Toast.LENGTH_LONG).show();
        }
        else {
          Toast.makeText(this, "Failed to add to Favorites", Toast.LENGTH_LONG).show();
        }

        currentFavNum = new GetCurrentFavorites().execute(gameName2).get(5, TimeUnit.SECONDS);

        counted =
            new UpdateFavoritesCount().execute(gameName2, String.valueOf(currentFavNum + 1)).get(5,
                TimeUnit.SECONDS);
        Toast.makeText(this, "Successful: " + counted, Toast.LENGTH_LONG).show();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      return true;
    default:
      return false;
    }
  }
}